﻿///////////////////////////////////////////////////////////////////////////////
// Copyright 2016 lingzimaike@gmail.com
// GPLv2
// Author: lingzimaike
///////////////////////////////////////////////////////////////////////////////

#ifndef PAPA_NET_HTTP_BACKEND_H_
#define PAPA_NET_HTTP_BACKEND_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <string>
#include <event2/event.h>
#include <event2/http.h>
#include <papa/base.h>
#include <papa/thread/thread_runnable.h>
#include <papa/thread/thread.h>

namespace papa {
namespace net {

    typedef void(*HTTP_CALLBACK)(void);

    class HttpBackend
    {
    public:
        HttpBackend(void);
        ~HttpBackend(void);

        bool Open(const ::std::string& host, int port);
        void Join(void);
        void Close(void);

    private:
        class IORunnable : public ::papa::thread::ThreadRunnable
        {
        public:
            IORunnable(void);
            virtual ~IORunnable(void);
            bool Open(void);
            void Stop(void);

            struct ::event_base* get_event_base(void) const { 
                return event_base_; 
            }

            virtual void Run(void);

        private:
            struct ::event_base* event_base_;

            PAPA_DISALLOW_COPY_AND_ASSIGN(IORunnable)
        };

        ::papa::thread::Thread io_thread_;

        PAPA_DISALLOW_COPY_AND_ASSIGN(HttpBackend)
    };

}}  // namespace papa::net

#endif // PAPA_NET_HTTP_BACKEND_H_
